package com.fresher.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.fresher.model.Accountinfo;
import com.fresher.model.Transaction;
import com.fresher.service.TransactionService;

@Controller
public class TransactionController {
	@Autowired
	TransactionService transactionService;
	// get create transaction view
		@RequestMapping(value = "/createtransaction", method = RequestMethod.GET)
		public ModelAndView CreateTransactionPage() {
			// access to spring security session
			Authentication auth = SecurityContextHolder.getContext().getAuthentication();
			UserDetails userDetail = (UserDetails) auth.getPrincipal();
			ModelAndView model = new ModelAndView();
			String loginId = userDetail.getUsername().toString();
			// get list account from accountService
			List<Accountinfo> accountinfo = transactionService.getListAccount(loginId);
			// list of account to
			List<Transaction> transactionlist = transactionService.getListRecentTransaction(loginId);
			List<String> translist = new ArrayList<String>();
			for (Transaction string : transactionlist) {
				translist.add("'" +  String.format("%010d", string.getAccountinfo2().getIdaccountInfo()) + "'");
			}
			//get list recent transaction for auto complete
			model.addObject("translist", translist);
			//list account for select list account from
			model.addObject("accountinfo", accountinfo);
			model.setViewName("createTransaction");
			return model;

		}

		// create transaction 
		@RequestMapping(value = "/createtrans", method = RequestMethod.POST)
		public ModelAndView addTransaction(HttpServletRequest request, Model model)
				throws ParseException {

			String SourceAccount;
			String description, dateString, amount, TargetAccount;
			// get account name 
			ModelAndView model1 = new ModelAndView();
			Authentication auth = SecurityContextHolder.getContext().getAuthentication();
			UserDetails userDetail = (UserDetails) auth.getPrincipal();
			String loginId = userDetail.getUsername().toString();
			
			// get list account from accountService
			List<Accountinfo> accountinfo = transactionService.getListAccount(loginId);
		

			// get list of recent transaction
			List<Transaction> transactionlist = transactionService.getListRecentTransaction(loginId);
			// ajax list of account to
			List<String> translist = new ArrayList<String>();
			for (Transaction string : transactionlist) {
				translist.add("'" +  String.format("%010d", string.getAccountinfo2().getIdaccountInfo()) + "'");
			}

			model1.addObject("translist", translist);

			// get source account and target account
			SourceAccount = request.getParameter("txtSourceAccount");
			TargetAccount = request.getParameter("txtTargetAccount");
			amount = request.getParameter("txtAmountTranfer");
			description = request.getParameter("txtDescription");
			dateString = request.getParameter("txtDate");
System.out.println(dateString);
			// create transaction by transactionService
			String mess = transactionService.createTransactionService(TargetAccount, SourceAccount, amount, description, dateString);
			//return message for view
			model1.addObject("mess", mess);
			//return value for each field from create transaction
			model1.addObject("accountinfo", accountinfo);
			model1.addObject("amount", amount);
			model1.addObject("targetAccount", TargetAccount);
			model1.addObject("description", description);
			model1.addObject("dateString", dateString);
			//return view createTransaction
			model1.setViewName("createTransaction");
			return model1;

		}

		// get view transaction
		@RequestMapping(value = "/viewtransaction", method = RequestMethod.GET)
		public ModelAndView ViewTransactionPage() {
			// access to spring security session
			Authentication auth = SecurityContextHolder.getContext().getAuthentication();
			UserDetails userDetail = (UserDetails) auth.getPrincipal();
			ModelAndView model = new ModelAndView();
			String loginId = userDetail.getUsername().toString();
			// get list transaction from service
			List<Transaction> TransactionList = transactionService.getListTransaction(loginId);
			List<Transaction> sendTransactionList = transactionService.getListSendTransaction(loginId);
			List<Transaction> receieveTransactionList = transactionService.getListReceiveTransaction(loginId);
			// account list from account service
			List<Accountinfo> accountList = transactionService.getListAccount(loginId);
			// send list to view
			model.addObject("AccountList", accountList);
			model.addObject("TransactionList", TransactionList);
			model.addObject("sendTransactionList", sendTransactionList);
			model.addObject("receiveTransactionList", receieveTransactionList);
			model.setViewName("viewTransaction");
			return model;

		}

		// get search transaction
		@RequestMapping(value = "/searchtransaction", method = RequestMethod.POST)
		public ModelAndView searchTransactionPage(HttpServletRequest request,
				Model model) {

			// access to spring security session
			Authentication auth = SecurityContextHolder.getContext().getAuthentication();
			UserDetails userDetail = (UserDetails) auth.getPrincipal();
			ModelAndView model1 = new ModelAndView();
			String loginId = userDetail.getUsername().toString();
			String start, end, transType;
			start = request.getParameter("date");
			end = request.getParameter("date1");
			transType = request.getParameter("transactionType");

			// get list transaction from service
			List<Transaction> TransactionList = transactionService.searchListTransaction(loginId, start, end, transType);
			List<Accountinfo> accountList = transactionService.getListAccount(loginId);
			// send list to view
			model1.addObject("AccountList", accountList);
			model1.addObject("startdate", start);
			model1.addObject("enddate", end);
			model1.addObject("TransactionList", TransactionList);
			model1.setViewName("viewTransaction");
			return model1;

		}
}